/**
 * Created by shidaiapp on 2017/9/29.
 */
import Vue from "vue";
import {isEmpty} from "../../../js/commonUtils";

export const init = (tiyData) => {
  var nowDate = new Date();
  tiyData.tParams.searchParams.choosedStartDate =nowDate;
  tiyData.tParams.searchParams.choosedEndDate = nowDate;
  tiyData.tParams.searchParams.startDate = Vue.prototype.$tiyQueryTime(new Date(nowDate).getTime());
  tiyData.tParams.searchParams.endDate = Vue.prototype.$tiyQueryTime(new Date(nowDate).getTime())
  tiyData.tStatus.searchedDateDesc = "(" + tiyData.tParams.searchParams.startDate + " - " + tiyData.tParams.searchParams.endDate + ")";
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const searchOrderCollect = (self, tiyData) => {
  self.$Spin.show();
  let startDate = getStartDate(tiyData.tParams.searchParams.choosedStartDate, self);
  let endDate = getEndDate(tiyData.tParams.searchParams.choosedEndDate, self);
  self.$apiAnalysis.orderCollect({
    "startDate" : startDate,
    "endDate" : endDate,
  }).then(function (response) {
    if (response.success) {
      self.$Spin.hide();
      tiyData.tStatus.searchedDateDesc = getDateDesc(self, tiyData.tParams.searchParams.choosedStartDate, tiyData.tParams.searchParams.choosedEndDate);
      let orderCollect = response.result;
      if (orderCollect.orderCollect != null){
        tiyData.tData.order.number.number = orderCollect.orderCollect.orderCount;
        tiyData.tData.order.amount.number = orderCollect.orderCollect.moneyCount;
        var salePostage = (orderCollect.orderCollect.postageCount / orderCollect.orderCollect.moneyCount).toFixed(2)
        tiyData.tData.order.amount.desc = "运费占比" + (salePostage * 100).toFixed(0)+ "%";
        tiyData.tData.order.product.number = orderCollect.orderCollect.productCount;
        tiyData.tData.order.product.desc = "合计" + orderCollect.orderCollect.productNum + "件产品";
      }
      if (orderCollect.orderDevice != null){
        tiyData.tData.order.devices.datas.rows.splice(0, tiyData.tData.order.devices.datas.rows.length);
        orderCollect.orderDevice.forEach(
          function (item) {
            tiyData.tData.order.devices.datas.rows.push({
              '设备': item.statisticName == null ? "Other" : item.statisticName,
              '访问用户': item.statisticCount
            });
          }
        );
      }
      // if (orderCollect.orderDelivery != null){
      //   tiyData.tData.order.shippings.datas.rows.splice(0, tiyData.tData.order.shippings.datas.rows.length);
      //   orderCollect.orderDelivery.forEach(
      //     function (item) {
      //       tiyData.tData.order.shippings.datas.rows.push({'物流' : item.statisticName,'订单数' : item.statisticCount});
      //     }
      //   );
      // }
      if (orderCollect.orderCountry != null){
        tiyData.tData.order.countries.datas.rows.splice(0, tiyData.tData.order.countries.datas.rows.length);
        orderCollect.orderCountry.forEach(
          function (item) {
            tiyData.tData.order.countries.datas.rows.push({
              '国家': item.statisticName == null ? "Other" : item.statisticName,
              '订单数': item.statisticCount
            });
          }
        );
      }
      if (orderCollect.orderLoginMethod != null){
        tiyData.tData.order.loginTypes.datas.rows.splice(0, tiyData.tData.order.loginTypes.datas.rows.length);
        orderCollect.orderLoginMethod.forEach(
          function (item) {
            tiyData.tData.order.loginTypes.datas.rows.push({
              '登陆方式': item.statisticName == null ? "Other" : item.statisticName,
              '订单数': item.statisticCount
            });
          }
        );
      }
      if (orderCollect.orderTimeInterval != null){
        tiyData.tData.order.timeRanges.datas.rows.splice(0, tiyData.tData.order.timeRanges.datas.rows.length);
        orderCollect.orderTimeInterval.forEach(
          function (item) {
            tiyData.tData.order.timeRanges.datas.rows.push({'时间区间' : item.statisticName,'订单数' : item.statisticCount});
          }
        );
      }
      if (orderCollect.orderClassification != null){
        tiyData.tData.order.productCategories.datas.rows.splice(0, tiyData.tData.order.productCategories.datas.rows.length);
        orderCollect.orderClassification.forEach(
          function (item) {
            tiyData.tData.order.productCategories.datas.rows.push({
              '产品分类': item.statisticName == null ? "Other" : item.statisticName,
              '子订单数': item.statisticCount
            });
          }
        );
      }
    } else {
      self.$Spin.hide();
      self.$Message.error(response.errorMessage);
    }
  });
};

const getDateDesc = (self, startDate, endDate) => {
  let startDateStr = isEmpty(startDate)?"?":self.$tiyBJTime(new Date(startDate).getTime(), "yyyy-MM-dd") + " 00:00:00";
  let endDateStr = isEmpty(endDate)?"现在":self.$tiyBJTime(new Date(endDate).getTime(), "yyyy-MM-dd") + " 23:59:59";
  return "("+startDateStr+"-"+endDateStr+")";
}

const getStartDate = (date, self) => {
  if(isEmpty(date)) {
    return null;
  }
  let dateStr = self.$tiyBJTime(new Date(date).getTime(), "yyyy-MM-dd") + " 00:00:00";
  return self.$tiyQueryTime(new Date(dateStr).getTime());
}

const getEndDate = (date, self) => {
  if(isEmpty(date)) {
    return null;
  }
  let dateStr = self.$tiyBJTime(new Date(date).getTime(), "yyyy-MM-dd") + " 23:59:59";
  return self.$tiyQueryTime(new Date(dateStr).getTime());
}

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const searchUserCollect = (self, tiyData) => {
  self.$Spin.show();
  let startDate = getStartDate(tiyData.tParams.searchParams.choosedStartDate, self);
  let endDate = getEndDate(tiyData.tParams.searchParams.choosedEndDate, self);

  self.$apiAnalysis.userCollect({
    "startDate" :startDate,
    "endDate" : endDate,
  }).then(function (response) {
    if (response.success) {
      self.$Spin.hide();
      let userCollect = response.result;
      if (userCollect.userCollect != null){
        tiyData.tData.user.number.number = userCollect.userCollect.userCount;
      }
      if (userCollect.userCountry != null){
        tiyData.tData.user.countries.datas.rows.splice(0, tiyData.tData.user.countries.datas.rows.length);
        userCollect.userCountry.forEach(
          function (item) {
            tiyData.tData.user.countries.datas.rows.push({
              '国家': item.statisticName == null ? "Other" : item.statisticName,
              '注册数': item.statisticCount
            });
          }
        );
      }
      if (userCollect.userLoginMethod != null){
        tiyData.tData.user.loginTypes.datas.rows.splice(0, tiyData.tData.user.loginTypes.datas.rows.length);
        userCollect.userLoginMethod.forEach(
          function (item) {
            tiyData.tData.user.loginTypes.datas.rows.push({
              '登陆方式': item.statisticName == null ? "Other" : item.statisticName,
              '注册数': item.statisticCount
            });
          }
        );
      }
    } else {
      self.$Spin.hide();
      self.$Message.error(response.errorMessage);
    }
  });
};
